//
//  Setting.m
//  RRQS
//
//  Created by kyjun on 16/3/29.
//
//

#import "Setting.h"
#import "JPUSHService.h"
#import <SVWebViewController/SVWebViewController.h>
#import "POISearchViewController.h"
#import "ReminderSettingViewController.h"
#import "TongZhiViewController.h"
#define FooterButton_Height 150


@interface Setting ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong) UIButton* btnLoginOut;

@property(nonatomic,strong) NSString* cellIdentifier;

@property(nonatomic,strong) NSArray* titleArray;

@end

@implementation Setting

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"设置";
    self.cellIdentifier = @"UITableViewCell";
    
    self.titleArray = @[@"我的定位",@"提示设置",@"管理细则",@"关于點吧骑手",@"版本号"];

    
    [self layoutUI];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark =====================================================  user interface layout
-(void)layoutUI{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,  CGRectGetHeight(self.view.frame) -NAVGATIONBAR_HEIGHT) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.separatorColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.tableFooterView = [self creatFooterView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:self.cellIdentifier];
    [self.view addSubview:self.tableView];


}

#pragma mark 提交按钮
-(UIView *)creatFooterView{
    
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, FooterButton_Height)];
    
    
    _btnLoginOut = [UIButton buttonWithType:UIButtonTypeCustom];
    _btnLoginOut.backgroundColor = [UIColor colorWithRed:213/255.f green:213/255.f blue:213/255.f alpha:1.0];
    [_btnLoginOut setTitleColor:[UIColor colorWithRed:109/255.f green:109/255.f blue:109/255.f alpha:1.0] forState:UIControlStateNormal];
    [_btnLoginOut setTitle:@"退出" forState:UIControlStateNormal];
    _btnLoginOut.layer.masksToBounds = YES;
    _btnLoginOut.layer.cornerRadius = 5.f;
    [_btnLoginOut addTarget:self action:@selector(loginOutTouch:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:_btnLoginOut];
    [_btnLoginOut mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(40);
        make.left.mas_equalTo(footerView).mas_offset(20);
        make.right.mas_equalTo(footerView).mas_offset(-20);
        make.centerY.mas_equalTo(footerView);
        make.centerX.mas_equalTo(footerView);

    }];
    
    
    return footerView;
}





#pragma mark =====================================================  <UITableViewDataSource>
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.textLabel.text = self.titleArray[indexPath.row];
    
    if( indexPath.row == self.titleArray.count - 1){
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        UILabel* label = [[UILabel alloc]init];
        label.textAlignment = NSTextAlignmentRight;
        label.frame = CGRectMake(SCREEN_WIDTH-SCREEN_WIDTH/3-10, 0, SCREEN_WIDTH/3, 50.f);
        [cell.contentView addSubview:label];
        
        
        NSDictionary *bundleDic = [[NSBundle mainBundle] infoDictionary];
        NSString* releaseVersion = [bundleDic objectForKey:@"CFBundleShortVersionString"];
        label.text = [NSString stringWithFormat:@"V%@",releaseVersion];
        
        
        
        
    }else{
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        
    }


   
    return cell;
}

#pragma mark =====================================================  <UITableViewDelegate>
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50.f;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {

        POISearchViewController *vc = [[POISearchViewController alloc] init];
        vc.title = @"我的定位";
        vc.returnPoiData = ^(NSDictionary *dic) {
            
//            NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
//
//            [userdefault setValue:dic[@"poi_latitude"] forKey:@"address_lat"];
//            [userdefault setValue:dic[@"poi_longitude"] forKey:@"address_long"];
            
        };
        
        [self.navigationController pushViewController:vc animated:YES];
        
    }
    
    if (indexPath.row == 1) {
        
        ReminderSettingViewController *VC = [[ReminderSettingViewController alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
        
        
    }
    
    
    if (indexPath.row == 2) {
                
        NSURL *URL = [NSURL URLWithString:@"http://xmpc.zadtek.com/mobile/help"];
        SVWebViewController *webViewController = [[SVWebViewController alloc] initWithURL:URL];
        [self.navigationController pushViewController:webViewController animated:YES];
        
        
    } else if (indexPath.row == 3) {
        TongZhiViewController* controller = [[TongZhiViewController alloc]init];
        controller.type = 2;
        [self.navigationController pushViewController:controller animated:YES];
    }
}


#pragma mark =====================================================  SEL
-(void)loginOutTouch:(UIButton*)sender{
    MMember* entity = self.Identity.userInfo;
    entity.isLogin = NO;
    Boolean flag= [NSKeyedArchiver archiveRootObject:entity toFile:[WMHelper archiverPath]];
    if(flag){
        [self logoutJPush:entity.userID];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else{
        
    }
}

#pragma mark =====================================================  private method
-(void)logoutJPush:(NSString*)userID{
    [JPUSHService deleteAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
        NSLog(@"消息推送退出%tu,%@,%tu",iResCode,iAlias,seq);
        if(iResCode == 0){
            
        }else{
            UIAlertView* alert = [[UIAlertView alloc]initWithTitle:nil message:@"消息推送退出失败!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
    } seq:1];
}


@end
